package com.aijia.custom.product.ui;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;

import com.aijia.custom.product.R;
import com.aijia.custom.product.customview.ClearEditText;
import com.aijia.custom.product.customview.LoadingWaitDialog;
import com.aijia.custom.product.customview.NoDoubleClickListener;
import com.aijia.custom.product.data.LoginResponse;
import com.aijia.custom.product.listener.ILoginListener;
import com.aijia.custom.product.manager.LoginManager;
import com.aijia.custom.product.utils.Md5Utils;
import com.aijia.custom.product.utils.Notification;
import com.aijia.custom.product.utils.SharedPreferencesUtils;
import com.aijia.custom.product.utils.Utils;

/**
 * Created by yejianjun on 2019/1/14.
 */

public class LoginActivity extends Activity implements ILoginListener {

    private Button loginBtn;
    private ClearEditText accountET;
    private ClearEditText passwordET;
    private LoginManager manager;
    private Dialog pDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        initLogin();
    }

    private void initView() {
        loginBtn = (Button) findViewById(R.id.login_btn);
        accountET = (ClearEditText) findViewById(R.id.login_username);
        passwordET = (ClearEditText) findViewById(R.id.login_password);
        loginBtn.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                switch (v.getId()) {
                    case R.id.login_btn:
                        // 隐藏软键盘
                        Utils.hideSoftInput(LoginActivity.this, accountET);
                        reqLogin();
                        break;

                    default:
                        break;
                }
            }
        });

    }

    private void initLogin() {
        manager = new LoginManager(this);
        manager.addLoginListener(this);
    }

    private void reqLogin() {
        showDialog();
        String userName = accountET.getText().toString().trim();
        String password = Md5Utils.Md5(passwordET.getText().toString().trim());
        manager.login(userName, password);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != manager) {
            manager.removeLoginListener(this);
        }
    }

    @Override
    public void onLoginFailure(int errorCode, String errorMsg) {
        if (null != pDialog && pDialog.isShowing()) {
            pDialog.dismiss();
        }
        Notification.toast(this, errorMsg == null ? "" : errorMsg);
    }

    @Override
    public void onLoginSuccess(LoginResponse response) {
        // 登录成功，保存用户名和密码
        String userName = accountET.getText().toString().trim();
        String password = Md5Utils.Md5(passwordET.getText().toString().trim());

        /**
         * 保存用户信息
         */
        SharedPreferencesUtils.setUserName(this, userName);
        SharedPreferencesUtils.setPassword(this, password);
        /**
         * 保存用户类型： 1. 店长，2，老板，3. 员工
         */
        SharedPreferencesUtils.setUserType(this, response.getStaff().getUseType());
        // 自动登录状态改成true
        SharedPreferencesUtils.setAutoLoginStatus(this, true);
        // 当前登录的用户ID
        SharedPreferencesUtils.setUserID(this, response.getStaff().getId());


        SharedPreferencesUtils.setOrganizeName(this, response.getStaff().getOrganizeName());
        SharedPreferencesUtils.setOrganizeId(this, response.getStaff().getOrganizeId());
        SharedPreferencesUtils.setToken(this, response.getToken());
        /**
         * 保存所有车间信息
         */

        if (null != pDialog && pDialog.isShowing()) {
            pDialog.dismiss();
        }
        intoMain(response.getStaff().getUseType());
    }

    private void intoMain(int type) {
        Intent intent = new Intent();
        intent.putExtra("user_type", type);
        intent.setClass(LoginActivity.this, MainActivity.class);
        startActivity(intent);
        this.finish();
    }

    private void showDialog() {
        pDialog = LoadingWaitDialog.createLoadingDialog(this, "");
        pDialog.show();
    }

}
